﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Helloworld.model;
using System.Diagnostics;
using Helloworld.FontsBitmap;
using Microsoft.Xna.Framework.Input.Touch;

namespace Helloworld
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        Texture2D background;
        Animation bagia;
        BitmapFont bt;
        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromMilliseconds(5);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
            
        }
        public void loadimage()
        {
            background = contentManager.Load<Texture2D>("Assets/troixanh1");
            bagia = new Animation(contentManager.Load<Texture2D>("Assets/tho con"),0.2f,4,2,100,100);
            //Debug.WriteLine("chao ban 111111111111111111111111222222222222222222222222222222222");
            //BitmapFont.BitmapFont.getFont("fontarial.txt");
            bt = new BitmapFont(contentManager.Load<Texture2D>("Assets/font/font-huongdan"), "font/font-huongdan.txt");
            bt.scale = 1f;
            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
            loadimage();
            // TODO: use this.content to load your game content here

            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
            double a = e.ElapsedTime.TotalSeconds;
            bagia.updateTime((float)a);

            TouchCollection touchLocations = TouchPanel.GetState();
            foreach (TouchLocation touchLocation in touchLocations)
            {
                if (touchLocation.State == TouchLocationState.Pressed)
                {
                    Vector2 touchPosition = touchLocation.Position;
                    if (touchPosition.X >= textPosition.X &&
                    touchPosition.X < textPosition.X + textSize.X &&
                    touchPosition.Y >= textPosition.Y &&
                    touchPosition.Y < textPosition.Y + textSize.Y)
                    {
                        textColor = new Color((byte)rand.Next(256),
                                              (byte)rand.Next(256),
                                              (byte)rand.Next(256));
                    }
                    
                }
            }
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            spriteBatch.Draw(background, new Rectangle(0,0,800,480), Color.White);
            bagia.draw(spriteBatch, new int[] { 0,1,2,3});
            bt.drawMutiLineRight(spriteBatch,"helloworld. where are you from? I'm find, thank you!",20,20,300);
            // TODO: Add your drawing code here
            spriteBatch.End();
        }
        
    }
}